#include <QMessageBox>

#include "userinfowidget.h"
#include "model/datacenter.h"
#include "mainwidget.h"


UserInfoWidget::UserInfoWidget(const UserInfo& userInfo, QWidget* parent)
    : QDialog(parent)
    , userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedSize(280, 320);
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(0);
    layout->setVerticalSpacing(20);
    layout->setContentsMargins(24, 0, 0, 40);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    // 3. 添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(60, 60);
    avatarBtn->setIconSize(QSize(60, 60));
    avatarBtn->setIcon(userInfo.avatar);

    QString labelStyle = "QLabel { font-size: 14px; font-weight: 700; color: rgb(127, 127, 127); "
                         "margin-left: 1px; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:hover { background-color: rgb(242, 242, 242); }";
    btnStyle += "QPushButton { text-align: bottom ; padding-bottom: 5px; color: rgb(87, 107, 149); "
                "font-size: 11px; font-weight: 500; }";

    int width = 80;
    int height = 30;

    // 4. 添加用户id
    idTag = new QLabel();
    idTag->setText("VQ");
    idTag->setFixedSize(30, height);
    idTag->setStyleSheet(labelStyle);
    idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userId);
    idLabel->setFixedSize(width, height);
    idLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 5. 添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(width, height);
    nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setFixedSize(width, height);
    nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(width, height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setFixedSize(width, height);
    phoneLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);


    // 7. 添加功能按钮

    QVBoxLayout *applyLayout = new QVBoxLayout();
    applyLayout->setSpacing(0);
    applyLayout->setContentsMargins(0, 0, 0, 16);

    QLabel *applyIconLabel = new QLabel();
    applyIconLabel->setFixedSize(100, 21);
    applyIconLabel->setPixmap(QIcon(":/resource/image/add_Fri.png").pixmap(21, 21));
    applyIconLabel->setStyleSheet("QLabel { margin-left: 26px; background-color: transparent; }");
    applyIconLabel->setAlignment(Qt::AlignTop);

    applyLayout->addWidget(applyIconLabel);
    applyLayout->setAlignment(Qt::AlignCenter);

    applyBtn = new QPushButton();
    applyBtn->setText("添加好友");
    applyBtn->setFixedSize(72, 50);
    applyBtn->setLayout(applyLayout);
    applyBtn->setStyleSheet(btnStyle);

    ////////////////////////////////////////////////////////////////

    QString sendBtnStyle = "QPushButton { border: none; background-color: transparent; }";
    sendBtnStyle += "QPushButton:hover { background-color: rgb(242, 242, 242); }";
    sendBtnStyle += "QPushButton { text-align: bottom ; padding-bottom: 5px; color: rgb(87, 107, 149); "
                "font-size: 11px; font-weight: 500; margin-left: 5px; }";

    QVBoxLayout *sendMesLayout = new QVBoxLayout();
    sendMesLayout->setSpacing(0);
    sendMesLayout->setContentsMargins(0, 0, 0, 16);

    QLabel *iconLabel = new QLabel();
    iconLabel->setFixedSize(100, 23);
    iconLabel->setPixmap(QIcon(":/resource/image/send_Mes.png").pixmap(23, 23));
    iconLabel->setStyleSheet("QLabel { margin-left: 24px; background-color: transparent; }");
    iconLabel->setAlignment(Qt::AlignTop);

    sendMesLayout->addWidget(iconLabel);
    sendMesLayout->setAlignment(Qt::AlignCenter);


    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(72, 50);
    sendMessageBtn->setStyleSheet(sendBtnStyle);
    sendMessageBtn->setLayout(sendMesLayout);


//////////////////////////////////////////////////////////////



    QVBoxLayout *delMesLayout = new QVBoxLayout();
    delMesLayout->setSpacing(0);
    delMesLayout->setContentsMargins(0, 0, 0, 16);

    QLabel* delconLabel = new QLabel();
    delconLabel->setFixedSize(100, 21);
    delconLabel->setPixmap(QIcon(":/resource/image/del_Fri.png").pixmap(21, 21));
    delconLabel->setStyleSheet("QLabel { margin-left: 26px; background-color: transparent; }");
    delconLabel->setAlignment(Qt::AlignTop);

    delMesLayout->addWidget(delconLabel);
    delMesLayout->setAlignment(Qt::AlignCenter);


    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(72, 50);
    deleteFriendBtn->setIcon(QIcon(""));
    deleteFriendBtn->setStyleSheet(btnStyle);
    deleteFriendBtn->setLayout(delMesLayout);

/////////////////////////////////////////////////////////////

    QWidget* spacer1 = new QWidget();
    spacer1->setFixedHeight(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedHeight(150);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);

    // 8. 添加到布局管理器
    layout->addWidget(spacer1, 0, 1, 1, 15);

    layout->addWidget(avatarBtn, 1, 0, 3, 2);

    layout->addWidget(idTag, 1, 2, 1, 1);
    layout->addWidget(idLabel, 1, 3, 1, 1);

    layout->addWidget(nameTag, 2, 2, 1, 1);
    layout->addWidget(nameLabel, 2, 3, 1, 1);

    layout->addWidget(phoneTag, 3, 2, 1, 1);
    layout->addWidget(phoneLabel, 3, 3, 1, 1);

    layout->addWidget(spacer2, 4, 1, 3, 15);

    layout->addWidget(applyBtn, 5, 0);
    layout->addWidget(sendMessageBtn, 5, 1);
    layout->addWidget(deleteFriendBtn, 5, 2);


    // 9. 初始化按钮的禁用关系
    DataCenter* dataCenter = DataCenter::getInstance();
    auto* myFriend = dataCenter->findFriendById(this->userInfo.userId);
    if (!myFriend)
    {
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
        sendMessageBtn->hide();
        deleteFriendBtn->hide();
    }
    else
    {
        applyBtn->setEnabled(false);
        applyBtn->hide();
    }

    // 10. 初始化信号槽
    initSignalSlot();

}

void UserInfoWidget::initSignalSlot()
{
    connect(sendMessageBtn, &QPushButton::clicked, this, [=](){
        // 获取主窗口
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(userInfo.userId);

        // 关闭本窗口
        this->close();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickDeleteFriendBtn);
    connect(applyBtn, &QPushButton::clicked, this, &UserInfoWidget::clickApplyBtn);
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    // 1. 弹出对话框 确认是否删除

    auto result = QMessageBox::warning(this, "删除好友", "确定删除该好友吗?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok)
    {
        LOG("取消删除好友");
        return;
    }

    // 2. 发送网络请求 实现删除好友功能
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(userInfo.userId);

    // 3. 关闭窗口
    this->close();
}

void UserInfoWidget::clickApplyBtn()
{
    // 1. 发送好友申请
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(userInfo.userId);
    // 2. 关闭窗口
    this->close();
}















